package com.yami.shop.webservice.common.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.SubScribeFundsPageDTO;
import com.yami.shop.bean.model.SubscribeFundsRecord;
import com.yami.shop.bean.vo.SubScribeFundsPageVo;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.webservice.common.dao.FundsRecordMapper;
import com.yami.shop.webservice.common.service.FundsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资金记录Service业务层处理
 *
 * @author zhangchaoqun
 * @date 2021-10-13
 */
@Service
public class FundsRecordServiceImpl implements FundsRecordService
{
    @Autowired
    private FundsRecordMapper fundsRecordMapper;
//    @Autowired
//    private TransferOrderRecordServiceImpl transferOrderRecordServiceImpl;
//    @Autowired
//    private ShopDetailFeignClient shopDetailFeignClient;
//    @Autowired
//    private IFundsRecordService iFundsRecordService;
//    @Autowired
//    private TransferOrderRecordService transferOrderRecordService;
//    @Autowired
//    private WapiFundService wapiFundService;

    /**
     * 查询资金记录
     *
     * @param id 资金记录主键
     * @return 资金记录
     */
    @Override
    public SubscribeFundsRecord selectFundsRecordById(String id)
    {
        return fundsRecordMapper.selectFundsRecordById(id);
    }

    /**
     * 查询资金记录列表
     *
     * @param fundsRecord 资金记录
     * @return 资金记录
     */
    @Override
    public List<SubscribeFundsRecord> selectFundsRecordList(SubscribeFundsRecord fundsRecord)
    {
        return fundsRecordMapper.selectFundsRecordList(fundsRecord);
    }

    /**
     * 新增资金记录
     *
     * @param fundsRecord 资金记录
     * @return 结果
     */
    @Override
    public int insertFundsRecord(SubscribeFundsRecord fundsRecord)
    {
        fundsRecord.setId(IdUtil.getSnowflake().nextIdStr());
        return fundsRecordMapper.insertFundsRecord(fundsRecord);
    }

    /**
     * 修改资金记录
     *
     * @param fundsRecord 资金记录
     * @return 结果
     */
    @Override
    public int updateFundsRecord(SubscribeFundsRecord fundsRecord)
    {
        return fundsRecordMapper.updateFundsRecord(fundsRecord);
    }

    /**
     * 批量删除资金记录
     *
     * @param ids 需要删除的资金记录主键
     * @return 结果
     */
    @Override
    public int deleteFundsRecordByIds(String[] ids)
    {
        return fundsRecordMapper.deleteFundsRecordByIds(ids);
    }

    /**
     * 删除资金记录信息
     *
     * @param id 资金记录主键
     * @return 结果
     */
    @Override
    public int deleteFundsRecordById(String id)
    {
        return fundsRecordMapper.deleteFundsRecordById(id);
    }



    /**
     * 分页
     * @param pageDTO
     * @param fundsRecord
     * @return
     */
    public PageVO<SubscribeFundsRecord> page(PageDTO pageDTO, SubscribeFundsRecord fundsRecord) {
        return PageUtil.doPage(pageDTO, () -> fundsRecordMapper.selectFundsRecordList(fundsRecord));
    }

    @Override
    public int insertFundsRecordList(List<SubscribeFundsRecord> list) {
        for (SubscribeFundsRecord fundsRecord:list) {
            fundsRecord.setId(IdUtil.getSnowflake().nextIdStr());
        }
        return fundsRecordMapper.insertFundsRecordList(list);
    }
//    /**
//     * 提货
//     *  orderId
//     */
//    @Override
//    @Transactional
//    public void getGoods(Long orderId) {
//        if(orderId==null){
//            throw new Mall4cloudException("订单id不能为空！");
//        }
////        FundsRecord fundsRecord = new FundsRecord();
////        fundsRecord.setOrderId(orderId);
////        List<FundsRecord> list = fundsRecordMapper.selectFundsRecordList(fundsRecord);
////        if(list==null|| list.size()==0){
////            throw new Mall4cloudException("获取不到该订单的信息！");
////        }
////        FundsRecord funds = list.get(0);
////        TransferOrderRecord transferOrderRecord = transferOrderRecordServiceImpl.selectTransferOrderRecordById(funds.getOutId());
//        TransferOrderRecord transferOrderRecord = transferOrderRecordServiceImpl.selectTransferOrderRecordByOrderId(orderId);
//        if(transferOrderRecord==null){
//            throw new Mall4cloudException("拨款信息未查到！");
//        }
//        if(StringUtils.isEmpty(transferOrderRecord.getbCode())){
//            throw new Mall4cloudException("查不到店铺编码！");
//        }
//        ServerResponseEntity<ShopDetailVO> shopListByShopCode = shopDetailFeignClient.getShopByShopCode(transferOrderRecord.getbCode());
//        if(!shopListByShopCode.isSuccess()){
//            throw new Mall4cloudException("获取商铺账户异常！");
//        }
//        if(shopListByShopCode.getData()==null || StringUtils.isEmpty(shopListByShopCode.getData().getShopAccount())){
//            throw new Mall4cloudException("店铺的交易所账号查询不到！");
//        }
//        String acount =  shopListByShopCode.getData().getShopAccount();
//        List<FundVo> listFun = new ArrayList<>();
//        String busiName = "店铺划款";
//        String outBizNo = transferOrderRecord.getId()+DateUtils.dateTimeNow("HHmmss");
//        //本地账单存储
//        List<FundsRecord> fundsRecordList = new ArrayList<>();
//        if(transferOrderRecord.getCommissio()!=null&&!transferOrderRecord.getCommissio().equals(BigDecimal.ZERO)){
//            //支付成交
//            //手续费原子
//            FundVo f1 = new FundVo();
//            f1.setExecuteNo("1");
//            f1.setSettlementAccount(acount);
//            f1.setSubTransCode(PayCode.TransferDeductionCommissionSubTrans.getCode());
//            f1.setTransCode(PayCode.TransferDeductionCommissionTrans.getCode());
//            BigDecimal deci = new BigDecimal(100).multiply(transferOrderRecord.getCommissio());
//            f1.setExchangeAmount(deci.intValue()+"");
//            //货款原子
//            FundVo f2 = new FundVo();
//            f2.setExecuteNo("2");
//            f2.setSettlementAccount(acount);
//            f2.setSubTransCode(PayCode.TransferPaymentSubTrans.getCode());
//            f2.setTransCode(PayCode.TransferPaymentTrans.getCode());
//            BigDecimal de = new BigDecimal(100).multiply(transferOrderRecord.getMoney());
//            f2.setExchangeAmount(de.intValue()+"");
//            listFun.add(f1);
//            listFun.add(f2);
//
//            //本地账单存储
//            FundsRecord fundsRecord1 = new FundsRecord();
//            fundsRecord1.setMoney(transferOrderRecord.getCommissio());
//            fundsRecord1.setbCode(transferOrderRecord.getbCode());
//            fundsRecord1.setTransDate(DateUtils.getNowDate());
//            fundsRecord1.setType(FundType.TransferDeductionCommissionType.getCode());
//            fundsRecord1.setOutId(outBizNo);
//            fundsRecord1.setOrderId(orderId);
//            fundsRecord1.setAccout(acount);
//            fundsRecordList.add(fundsRecord1);
//            FundsRecord fundsRecord2 = new FundsRecord();
//            fundsRecord2.setMoney(transferOrderRecord.getMoney());
//            fundsRecord2.setbCode(transferOrderRecord.getbCode());
//            fundsRecord2.setTransDate(DateUtils.getNowDate());
//            fundsRecord2.setType(FundType.TransferPaymentType.getCode());
//            fundsRecord2.setOutId(outBizNo);
//            fundsRecord2.setOrderId(orderId);
//            fundsRecord1.setAccout(acount);
//            fundsRecordList.add(fundsRecord2);
//        }else{
//            //货款原子
//            FundVo f2 = new FundVo();
//            f2.setExecuteNo("1");
//            f2.setSettlementAccount(acount);
//            f2.setSubTransCode(PayCode.TransferPaymentSubTrans.getCode());
//            f2.setTransCode(PayCode.TransferPaymentTrans.getCode());
//            BigDecimal dec = new BigDecimal(100).multiply(transferOrderRecord.getMoney());
//            f2.setExchangeAmount(dec.intValue()+"");
//            listFun.add(f2);
//
//            //本地账单存储
//            FundsRecord fundsRecord1 = new FundsRecord();
//            fundsRecord1.setMoney(transferOrderRecord.getMoney());
//            fundsRecord1.setbCode(transferOrderRecord.getbCode());
//            fundsRecord1.setTransDate(DateUtils.getNowDate());
//            fundsRecord1.setType(FundType.TransferPaymentType.getCode());
//            fundsRecord1.setOutId(outBizNo);
//            fundsRecord1.setOrderId(orderId);
//            fundsRecord1.setAccout(acount);
//            fundsRecordList.add(fundsRecord1);
//        }
//        TransferOrderRecord param = new TransferOrderRecord();
//        param.setId(transferOrderRecord.getId());
//        param.setStatus(1);
//        param.setToSettleAccount(acount);
//        transferOrderRecordService.updateFTransferOrderRecord(param);
//        //本地账单存储
//        iFundsRecordService.insertFundsRecordList(fundsRecordList);
//        //最终掉支付
//        wapiFundService.fundPay(outBizNo,busiName,listFun);
//    }

//    @Override
//    @Transactional
//    public String refunds(Long orderId) {
//        if(orderId==null){
//            throw new Mall4cloudException("订单id不能为空！");
//        }
//        FundsRecord fundsRecord = new FundsRecord();
//        fundsRecord.setOrderId(orderId);
//        List<FundsRecord> list = fundsRecordMapper.selectFundsRecordList(fundsRecord);
//        if(list==null|| list.size()==0){
//            throw new Mall4cloudException("获取不到该订单的信息！");
//        }
//        FundsRecord fundsRecord1 = list.get(0);
//
//        List<FundVo> listFun = new ArrayList<>();
//        String busiName = "货款退款";
//        BigDecimal money = null;
//        if(fundsRecord1.getType().equals(FundType.PayPaymentType.getCode())){
//            money = fundsRecord1.getMoney();
//        }else if(list.size()>1&&list.get(1)!=null&&FundType.PayPaymentType.getCode().equals(list.get(1).getType())){
//            money = list.get(1).getMoney();
//            fundsRecord1 = list.get(1);
//        }
//        TransferOrderRecord param = new TransferOrderRecord();
//        if(fundsRecord1.getOutId().length()<7){
//            throw new Mall4cloudException("该订单资金信息记录异常！");
//        }
//        String id = fundsRecord1.getOutId().substring(0,fundsRecord1.getOutId().length()-6);
//        String outBizNo = id+DateUtils.dateTimeNow("HHmmss");
//        param.setId(id);
//        param.setStatus(2);
//        transferOrderRecordService.updateFTransferOrderRecord(param);
//
//        String acount =  fundsRecord1.getAccout();
//        //本地账单存储
//        List<FundsRecord> fundsRecordList = new ArrayList<>();
//            //货款原子
//        FundVo f2 = new FundVo();
//        f2.setExecuteNo("1");
//        f2.setSettlementAccount(acount);
//        f2.setSubTransCode(PayCode.BackPaymentTypeSubTrans.getCode());
//        f2.setTransCode(PayCode.BackPaymentTypeTrans.getCode());
//        BigDecimal dec = new BigDecimal(100).multiply(money);
//        f2.setExchangeAmount(dec.intValue()+"");
//        listFun.add(f2);
//
////        //本地账单存储
//        FundsRecord fundsRecord2 = new FundsRecord();
//        fundsRecord2.setTenantId(fundsRecord1.getTenantId());
//        fundsRecord2.setMoney(money);
//        fundsRecord2.setUserId(fundsRecord1.getUserId());
//        fundsRecord2.setTransDate(DateUtils.getNowDate());
//        fundsRecord2.setType(FundType.BackPaymentType.getCode());
//        fundsRecord2.setOutId(outBizNo);
//        fundsRecord2.setOrderId(orderId);
//        fundsRecord2.setAccout(acount);
//        fundsRecordList.add(fundsRecord2);
//        //本地账单存储
//        iFundsRecordService.insertFundsRecordList(fundsRecordList);
//        //最终掉支付
//        wapiFundService.fundPay(outBizNo,busiName,listFun);
//        return outBizNo;
//    }

    /**
     * 分页
     * @param pageDTO
     * @param fundsRecord
     * @return
     */
    public PageVO<SubscribeFundsRecord> page1(PageDTO pageDTO, SubscribeFundsRecord fundsRecord) {
        return PageUtil.doPage(pageDTO, () -> fundsRecordMapper.selectFundsRecordList1(fundsRecord));
    }

    /**
     * 分页
     * @param fundsRecord
     * @return
     */
    public List<SubscribeFundsRecord> selectFundsRecordList1(SubscribeFundsRecord fundsRecord) {
        return fundsRecordMapper.selectFundsRecordList1(fundsRecord);
    }

    @Override
    public IPage<SubScribeFundsPageVo> pageList(PageParam<SubscribeFundsRecord> pageParam, SubScribeFundsPageDTO dto) {
        return fundsRecordMapper.pageList(pageParam,dto);
    }
}
